﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NetPE.Core.Pe;
using System.Collections.ObjectModel;

namespace NetPE
{
    public partial class SectionEditor : BaseEditor
    {
        private class CollectionLinkingBindingList<T> : BindingList<T>
        {
            public CollectionLinkingBindingList(Collection<T> coll) : base(coll) { }

            protected override void ClearItems()
            {
                base.ClearItems();
            }
            protected override void InsertItem(int index, T item)
            {
                base.InsertItem(index, item);
            }
            protected override void RemoveItem(int index)
            {
                base.RemoveItem(index);
            }
            protected override void SetItem(int index, T item)
            {
                base.SetItem(index, item);
            }
        }


        private SplitContainer splitContainer1;
        private ListBox sects;
        private PropertyEditor prop;
    
        public SectionEditor()
        {
            InitializeComponent();

            sects.SelectedIndexChanged += new EventHandler(ItemChanged);
            sects.ContextMenuStrip = new ContextMenuStrip();
            ToolStripMenuItem add = new ToolStripMenuItem("Add");
            add.Click += new EventHandler(Add);
            sects.ContextMenuStrip.Items.Add(add);
            ToolStripMenuItem del = new ToolStripMenuItem("Delete");
            del.Click += new EventHandler(Delete);
            sects.ContextMenuStrip.Items.Add(del);

            prop.PropertyChanged += new EventHandler(RefreshText);
        }

        void InitializeComponent()
        {
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.sects = new System.Windows.Forms.ListBox();
            this.prop = new NetPE.PropertyEditor();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.SuspendLayout();
            // 
            // splitContainer1
            // 
            this.splitContainer1.BackColor = System.Drawing.Color.White;
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.Location = new System.Drawing.Point(0, 0);
            this.splitContainer1.Name = "splitContainer1";
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.sects);
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.BackColor = System.Drawing.Color.Black;
            this.splitContainer1.Panel2.Controls.Add(this.prop);
            this.splitContainer1.Size = new System.Drawing.Size(461, 389);
            this.splitContainer1.SplitterDistance = 75;
            this.splitContainer1.TabIndex = 0;
            // 
            // sects
            // 
            this.sects.BackColor = System.Drawing.Color.Black;
            this.sects.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.sects.Dock = System.Windows.Forms.DockStyle.Fill;
            this.sects.ForeColor = System.Drawing.Color.White;
            this.sects.FormattingEnabled = true;
            this.sects.IntegralHeight = false;
            this.sects.Location = new System.Drawing.Point(0, 0);
            this.sects.Name = "sects";
            this.sects.Size = new System.Drawing.Size(100, 389);
            this.sects.TabIndex = 0;
            // 
            // prop
            // 
            this.prop.AutoScroll = true;
            this.prop.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
            this.prop.Dock = System.Windows.Forms.DockStyle.Fill;
            this.prop.Font = new System.Drawing.Font("Arial", 9F);
            this.prop.Location = new System.Drawing.Point(0, 0);
            this.prop.Name = "prop";
            this.prop.Object = null;
            this.prop.Size = new System.Drawing.Size(357, 389);
            this.prop.TabIndex = 0;
            this.prop.Text = "propertyEditor1";
            // 
            // SectionEditor
            // 
            this.Controls.Add(this.splitContainer1);
            this.Name = "SectionEditor";
            this.Size = new System.Drawing.Size(461, 389);
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            this.splitContainer1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        void Delete(object sender, EventArgs e)
        {
            if (sects.SelectedItem != null)
            {
                (sects.DataSource as BindingList<Section>).Remove(sects.SelectedItem as Section);
            }
        }

        void Add(object sender, EventArgs e)
        {
            (sects.DataSource as BindingList<Section>).Add(new Section(Object as SectionHeaders) { Name = "newSect" });
        }

        protected override void RefreshData()
        {
            SectionHeaders hdr = Object as SectionHeaders;
            CollectionLinkingBindingList<Section> bList = new CollectionLinkingBindingList<Section>(hdr);
            sects.DisplayMember = "Name";
            sects.DataSource = bList;
            prop.Object = null;
            prop.Visible = false;
        }

        private void ItemChanged(object sender, EventArgs e)
        {
            if (sects.SelectedItem != null)
            {
                prop.Visible = false;
                prop.Object = sects.SelectedItem as Section;
                prop.Visible = true;
            }
        }

        private void RefreshText(object sender, EventArgs e)
        {
            (sects.DataSource as BindingList<Section>).ResetBindings();
        }
    }
}
